#pragma once
#include "../server.hpp"
#include <fstream>
#include <sys/stat.h>
#include <regex>

//这些信息在文档里查看
std::unordered_map<int, std::string> _statu_msg = {
    {100,  "Continue"},
    {101,  "Switching Protocol"},
    {102,  "Processing"},
    {103,  "Early Hints"},
    {200,  "OK"},
    {201,  "Created"},
    {202,  "Accepted"},
    {203,  "Non-Authoritative Information"},
    {204,  "No Content"},
    {205,  "Reset Content"},
    {206,  "Partial Content"},
    {207,  "Multi-Status"},
    {208,  "Already Reported"},
    {226,  "IM Used"},
    {300,  "Multiple Choice"},
    {301,  "Moved Permanently"},
    {302,  "Found"},
    {303,  "See Other"},
    {304,  "Not Modified"},
    {305,  "Use Proxy"},
    {306,  "unused"},
    {307,  "Temporary Redirect"},
    {308,  "Permanent Redirect"},
    {400,  "Bad Request"},
    {401,  "Unauthorized"},
    {402,  "Payment Required"},
    {403,  "Forbidden"},
    {404,  "Not Found"},
    {405,  "Method Not Allowed"},
    {406,  "Not Acceptable"},
    {407,  "Proxy Authentication Required"},
    {408,  "Request Timeout"},
    {409,  "Conflict"},
    {410,  "Gone"},
    {411,  "Length Required"},
    {412,  "Precondition Failed"},
    {413,  "Payload Too Large"},
    {414,  "URI Too Long"},
    {415,  "Unsupported Media Type"},
    {416,  "Range Not Satisfiable"},
    {417,  "Expectation Failed"},
    {418,  "I'm a teapot"},
    {421,  "Misdirected Request"},
    {422,  "Unprocessable Entity"},
    {423,  "Locked"},
    {424,  "Failed Dependency"},
    {425,  "Too Early"},
    {426,  "Upgrade Required"},
    {428,  "Precondition Required"},
    {429,  "Too Many Requests"},
    {431,  "Request Header Fields Too Large"},
    {451,  "Unavailable For Legal Reasons"},
    {501,  "Not Implemented"},
    {502,  "Bad Gateway"},
    {503,  "Service Unavailable"},
    {504,  "Gateway Timeout"},
    {505,  "HTTP Version Not Supported"},
    {506,  "Variant Also Negotiates"},
    {507,  "Insufficient Storage"},
    {508,  "Loop Detected"},
    {510,  "Not Extended"},
    {511,  "Network Authentication Required"}
};

std::unordered_map<std::string, std::string> _mime_msg = {
    {".aac",        "audio/aac"},
    {".abw",        "application/x-abiword"},
    {".arc",        "application/x-freearc"},
    {".avi",        "video/x-msvideo"},
    {".azw",        "application/vnd.amazon.ebook"},
    {".bin",        "application/octet-stream"},
    {".bmp",        "image/bmp"},
    {".bz",         "application/x-bzip"},
    {".bz2",        "application/x-bzip2"},
    {".csh",        "application/x-csh"},
    {".css",        "text/css"},
    {".csv",        "text/csv"},
    {".doc",        "application/msword"},
    {".docx",       "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
    {".eot",        "application/vnd.ms-fontobject"},
    {".epub",       "application/epub+zip"},
    {".gif",        "image/gif"},
    {".htm",        "text/html"},
    {".html",       "text/html"},
    {".ico",        "image/vnd.microsoft.icon"},
    {".ics",        "text/calendar"},
    {".jar",        "application/java-archive"},
    {".jpeg",       "image/jpeg"},
    {".jpg",        "image/jpeg"},
    {".js",         "text/javascript"},
    {".json",       "application/json"},
    {".jsonld",     "application/ld+json"},
    {".mid",        "audio/midi"},
    {".midi",       "audio/x-midi"},
    {".mjs",        "text/javascript"},
    {".mp3",        "audio/mpeg"},
    {".mpeg",       "video/mpeg"},
    {".mpkg",       "application/vnd.apple.installer+xml"},
    {".odp",        "application/vnd.oasis.opendocument.presentation"},
    {".ods",        "application/vnd.oasis.opendocument.spreadsheet"},
    {".odt",        "application/vnd.oasis.opendocument.text"},
    {".oga",        "audio/ogg"},
    {".ogv",        "video/ogg"},
    {".ogx",        "application/ogg"},
    {".otf",        "font/otf"},
    {".png",        "image/png"},
    {".pdf",        "application/pdf"},
    {".ppt",        "application/vnd.ms-powerpoint"},
    {".pptx",       "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
    {".rar",        "application/x-rar-compressed"},
    {".rtf",        "application/rtf"},
    {".sh",         "application/x-sh"},
    {".svg",        "image/svg+xml"},
    {".swf",        "application/x-shockwave-flash"},
    {".tar",        "application/x-tar"},
    {".tif",        "image/tiff"},
    {".tiff",       "image/tiff"},
    {".ttf",        "font/ttf"},
    {".txt",        "text/plain"},
    {".vsd",        "application/vnd.visio"},
    {".wav",        "audio/wav"},
    {".weba",       "audio/webm"},
    {".webm",       "video/webm"},
    {".webp",       "image/webp"},
    {".woff",       "font/woff"},
    {".woff2",      "font/woff2"},
    {".xhtml",      "application/xhtml+xml"},
    {".xls",        "application/vnd.ms-excel"},
    {".xlsx",       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
    {".xml",        "application/xml"},
    {".xul",        "application/vnd.mozilla.xul+xml"},
    {".zip",        "application/zip"},
    {".3gp",        "video/3gpp"},
    {".3g2",        "video/3gpp2"},
    {".7z",         "application/x-7z-compressed"}
};

class Util
{
public:
    //字符串分割函数，将src字符串按照sep字符进行分割，得到的各个子串放到arry中，最终返回子串的数量
    static size_t Split(const std::string& src, const std::string& sep, std::vector<std::string>* arry)
    {
        size_t offset = 0;
        while (offset < src.size())
        {
            size_t pos = src.find(sep, offset);
            if (pos == src.npos)
            {
                if (pos == src.size())
                    break;
                arry->push_back(src.substr(offset));
                return arry->size();
            }
            if (pos != offset)//如果不是一个空串，就加入结果
                arry->push_back(src.substr(offset, pos - offset));
            offset = pos + sep.size();
        }
        return arry->size();
    }
    //读取文件内容，将读取的内容放到一个Buffer中
    static bool ReadFile(const std::string& filename, std::string* buf)
    {
        std::ifstream ifs(filename, std::ios::binary);
        if (!ifs.is_open())
        {
            printf("open %s file failed!!", filename.c_str());
            return false;
        }
        size_t fsize = 0;
        ifs.seekg(0, ifs.end);  //将文件偏移移动到文件的末尾
        fsize = ifs.tellg();    //此时偏移的位置就是文件的大小
        ifs.seekg(0, ifs.beg);  //将文件偏移移动到文件起始位置
        buf->resize(fsize);
        ifs.read(&(*buf)[0], fsize);
        if (ifs.good() == false)    //判断上次操作是否出问题
        {
            printf("read %s file failed!!", filename.c_str());
            ifs.close();
            return false;
        }
        ifs.close();
        return true;
    }
    //向文件写入数据
    static bool WriteFile(const std::string& filename, const std::string& buf)
    {
        std::ofstream ofs(filename, std::ios::binary | std::ios::trunc);    //trunc: 截断，不要文件的原有内容，即覆盖写
        if (!ofs.is_open())
        {
            printf("open %s file failed!!", filename.c_str());
            return false;
        }
        ofs.write(buf.c_str(), buf.size());
        if (ofs.good() == false)
        {
            ERR_LOG("write %s file failed!", filename.c_str());
            ofs.close();
            return false;
        }
        ofs.close();
        return true;
    }
    //URL编码
    static std::string UrlEncode(const std::string url, bool convert_space_to_plus)
    {
        std::string res;
        for (auto& c : url)
        {
            if (c == '.' || c == '-' || c == '_' || c == '~' || isalnum(c))
            {
                res += c;
                continue;
            }
            else if (c == ' ' && convert_space_to_plus == true)
            {
                res += '+';
                continue;
            }
            //剩下的字符都是需要编码成为%HH格式
            char tmp[4];
            snprintf(tmp, 4, "%%%02X", c);//前两个%表示一个%，后面的是%02X表示用十六进制表示
            res += tmp;
        }
        return res;
    }
    static char HEXTOI(char c)
    {
        if (c >= '0' && c <= '9')
            return c - '0';
        else if (c >= 'a' && c <= 'z')
            return c - 'a' + 10;
        else if (c >= 'A' && c <= 'Z')
            return c - 'A' + 10;
        return -1;
    }
    //URL解码
    static std::string UrlDecode(const std::string url, bool convert_plus_to_space)
    {
        std::string res;
        for (int i = 0; i < url.size(); ++i)
        {
            if (url[i] == '%' && i + 2 < url.size())
            {
                char v1 = HEXTOI(url[i + 1]);
                char v2 = HEXTOI(url[i + 2]);
                char v = (v1 * 16) + v2;
                res += v;
                i += 2;
                continue;
            }
            if (convert_plus_to_space == true && url[i] == '+')
            {
                res += ' ';
                continue;
            }
            res += url[i];
        }
        return res;
    }
    //响应状态码的描述信息获取
    static std::string StatuDesc(int statu)
    {
        auto it = _statu_msg.find(statu);
        if (it != _statu_msg.end())
        {
            return it->second;
        }
        return "Unkown";
    }
    //根据文件后缀名获取文件mime
    static std::string ExMime(const std::string& filename)
    {
        size_t pos = filename.find_last_of('.');
        if (pos == std::string::npos)
        {
            return "application/occtet-stream";
        }
        //根据扩展名，获取mime
        std::string ext = filename.substr(pos);
        auto it = _mime_msg.find(ext);
        if (it == _mime_msg.end())
        {
            return "application/occtet-stream";
        }
        return it->second;
    }
    //判断一个文件是否是一个目录
    static bool IsDirectory(const std::string& filename)
    {
        struct stat st;
        int ret = stat(filename.c_str(), &st);  //获取到文件属性
        if (ret < 0)
        {
            return false;
        }
        return S_ISDIR(st.st_mode);
    }
    //判断一个文件是否是一个普通文件
    static bool IsRegular(const std::string& filename)
    {
        struct stat st;
        int ret = stat(filename.c_str(), &st);  //获取到文件属性
        if (ret < 0)
        {
            return false;
        }
        return S_ISREG(st.st_mode);
    }
    //http请求的资源路径有效性判断
    static bool ValidPath(const std::string& path)
    {
        std::vector<std::string> subdir;
        Util::Split(path, "/", &subdir);
        int level = 0;
        for (auto& dir : subdir)
        {
            if (dir == "..")
            {
                level--;
                if (level < 0)//任意一层走出相对根目录，就认为有问题
                {
                    return false;
                }
            }
            level++;
        }
        return true;
    }
};

class HttpRequest
{
public:
    std::string _method;    //请求方法
    std::string _path;      //资源路径
    std::string _version;   //协议版本
    std::string _body;      //请求正文
    std::smatch _matches;   //资源路径的正则提取数据
    std::unordered_map<std::string, std::string> _headers;  //头部字段
    std::unordered_map<std::string, std::string> _params;   //查询字符串
public:
    HttpRequest()
    :_version("HTTP/1.1")
    {}
    //重置 -- 每一次上下文里面的内容处理完了我们就要重置一下，因为不重置，就代表这个请求还在处理，这次的信息就会对下次的请求信息造成影响
    void ReSet()
    {
        _method.clear();
        _path.clear();
        _version = "HTTP/1.1";
        _body.clear();
        std::smatch match;  //因为smatch没有clear函数，通过swap也可以达到clear的效果
        _matches.swap(match);
        _headers.clear();
        _params.clear();
    }
    //插入头部字段
    void SetHeader(const std::string& key, const std::string& val)
    {
        _headers.insert({key, val});
    }
    //判断是否存在指定头部字段
    bool HasHeader(const std::string& key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
            return false;
        return true;
    }
    //获取指定头部字段
    std::string GetHeader(const std::string& key) const 
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
        {
            return "";
        }
        return it->second;
    }
    //插入查询字符串
    void SetParam(std::string& key, std::string& val)
    {
        _params.insert({key, val});
    }
    //判断是否有某个指定的查询字符串
    bool HasParam(std::string& key)
    {
        auto it = _params.find(key);
        if (it == _params.end())
            return false;
        return true;
    }
    //获取指定的查询字符串
    std::string GetParam(std::string& key)
    {
        auto it = _params.find(key);
        if (it == _params.end())
        {
            return "";
        }
        return it->second;
    }
    //获取正文长度
    size_t ContentLength()
    {
        bool ret = HasHeader("Content-Length");
        if (ret == false)
            return 0;
        return std::stoi(GetHeader("Content-Length"));
    }
    //判断是否是短连接
    bool Close() const
    {
        if (GetHeader("Connection") == "keep-alive")
            return false;
        return true;
    }
};

class HttpResponse
{
public:
    int _statu;                 //状态码
    bool _redirect_flag;        //是否重定向
    std::string _body;          //响应正文
    std::string _redirect_url;  //重定向路径
    std::unordered_map<std::string, std::string> _headers;  //响应头部字段
public:
    HttpResponse(int statu = 200)
    :_redirect_flag(false), _statu(statu)
    {}
    void ReSet()
    {
        _statu = 200;
        _redirect_flag = false;
        _body.clear();
        _redirect_url.clear();
        _headers.clear();
    }
    //插入头部字段
    void SetHeader(const std::string& key, const std::string& val)
    {
        _headers.insert({key, val});
    }
    //判断是否存在指定头部字段
    bool HasHeader(const std::string& key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
            return false;
        return true;
    }
    //获取指定头部字段
    std::string GetHeader(const std::string& key)
    {
        auto it = _headers.find(key);
        if (it == _headers.end())
        {
            return "";
        }
        return it->second;
    }
    void SetContent(const std::string& body, const std::string& type = "text/html")
    {
        _body = body;
        SetHeader("Content-Type", type);
    }
    void SetRedirect(const std::string& url, int statu = 302)
    {
        _statu = statu;
        _redirect_flag =  true;
        _redirect_url = url;
    }
    //判断是否是短连接
    bool Close()
    {
        if (GetHeader("Connection") == "keep-alive")
            return false;
        return true;
    }
};


typedef enum
{
    RECV_HTTP_ERROR,    //出错
    RECV_HTTP_LINE,     //请求行
    RECV_HTTP_HEAD,     //请求头部
    RECV_HTTP_BODY,     //请求正文
    RECV_HTTP_OVER      //结束阶段
}HttpRecvStatu;

#define MAX_LINE 81920
class HttpContext
{
public:
    int _resp_statu;            //响应状态码
    HttpRecvStatu _recv_statu;  //当前接收机解析的阶段状态
    HttpRequest _request;       //已经解析得到的请求信息
private:
    bool ParseHttpLine(const std::string& line)
    {
        std::smatch matches;
        std::regex e("(GET|HEAD|POST|PUT|DELETE) ([^?]*)(?:\\?(.*))? (HTTP/1\\.[01])(?:\n|\r\n)?", std::regex::icase);//icase忽略大小写
        bool ret = std::regex_match(line, matches, e);
        if (ret == false)
        {
            _recv_statu = RECV_HTTP_ERROR;
            _resp_statu = 400;  //BAD REQUEST
            return false;
        }
        // 0: GET /biejiuyeke/login?user=xiaoming&passwd=123123 HTTP/1.1 size:60
        // 1: GET size:3
        // 2: /biejiuyeke/login size:17
        // 3: user=xiaoming&passwd=123123 size:27
        // 4: HTTP/1.1 size:8
        //第0个是url本身，第一个是请求方法，第二个是资源路径，第三个是查询字符串，第四个是协议版本

        //请求方法的获取
        _request._method = matches[1];
        std::transform(_request._method.begin(), _request._method.end(), _request._method.begin(), ::toupper);//字母都转为大写
        //查询路径可能也是经过url编码的，资源路径的获取，需要进行URL解码操作，但是不需要+转空格
        _request._path = Util::UrlDecode(matches[2], false);
        //协议版本获取
        _request._version = matches[4];
        //查询字符串的获取与处理
        std::vector<std::string> query_string_arry;
        std::string query_string = matches[3];
        //查询字符串的格式key=val&key=val&key=val...，以&符号进行分割，得到各个子串
        Util::Split(query_string, "&", &query_string_arry);
        //针对各个子串，以=符号进行分割，得到key和val，得到之后也需要进行URL解码
        for (auto& str : query_string_arry)
        {
            size_t pos = str.find("="); //查询字符串都是一种key=value的字符串
            if (pos == std::string::npos)
            {
                _recv_statu = RECV_HTTP_ERROR;
                _resp_statu = 400;  //BAD REQUEST
                return false;
            }
            std::string key = Util::UrlDecode(str.substr(0, pos), true);
            std::string val = Util::UrlDecode(str.substr(pos + 1), true);
            _request.SetParam(key, val);
        }
        return true;
    }
    //接收请求行
    bool RecvHttpLine(Buffer* buf)
    {
        if (_recv_statu != RECV_HTTP_LINE)
            return false;
        //1。获取一行数据，带有末尾的换行
        std::string line = buf->GetLineAndPop();    //这就是为什么之前设计的时候读取带有末尾的换行
        //2。需要考虑的一些要素：缓冲区中的数据不足一行或者获取一行的数据超大
        if (line.size() == 0)
        {
            //说明缓冲区中数据不足一行
            if (buf->ReadAbleSize() > MAX_LINE) //判断缓冲区中的可读数据长度，如果很长了都不足一行，说明有问题
            {
                _recv_statu = RECV_HTTP_ERROR;
                _resp_statu = 414;  //URI TOO LONG
                return false;
            }
            //缓冲区中的数据不足一行，长度也合理，就等下一次的新数据到来再处理
            return true;
        }
        if (line.size() > MAX_LINE)
        {
            _recv_statu = RECV_HTTP_ERROR;
            _resp_statu = 414;  //URI TOO LONG
            return false;
        }
        //走到这里说明得到了完整的请求行，就可以进行解析了
        bool ret = ParseHttpLine(line);
        if (ret == false)
            return false;
        //首行处理完毕，进入头部获取阶段
        _recv_statu = RECV_HTTP_HEAD;
        return true;
    }
    bool ParseHttpHead(std::string& line)
    {
        if (line.back() == '\n')
            line.pop_back();
        if (line.back() == '\r')
            line.pop_back();
        size_t pos = line.find(": ");
        if (pos == line.npos)
        {
            _recv_statu = RECV_HTTP_ERROR;
            _resp_statu = 400;
            return false;
        }
        std::string key = line.substr(0, pos);
        std::string val = line.substr(pos + 2);
        _request.SetHeader(key, val);
        return true;
    }
    //接收请求头部字段
    bool RecvHttpHead(Buffer* buf)
    {
        if (_recv_statu != RECV_HTTP_HEAD)
            return false;
        while (1)
        {
            //一行一行取出数据，知道遇到空行为止，头部格式 key: val\r\nkey: val\r\n...
            //1。获取一行数据
            std::string line = buf->GetLineAndPop();
            //2。需要考虑的一些要素：缓冲区中的数据不足一行，获取的一行数据超大
            if (line.size() == 0)
            {
                //缓冲区中的数据不足一行，则需要判断缓冲区的可读数据长度，如果很长都不足一行，这是有问题的
                if (buf->ReadAbleSize() > MAX_LINE)
                {
                    _recv_statu = RECV_HTTP_ERROR;
                    _resp_statu = 414;
                    return false;
                }
                //缓冲区中数据不足一行，符合长度预期，就等待新数据的到来
                return true;
            }
            if (line.size() > MAX_LINE)
            {
                _recv_statu = RECV_HTTP_ERROR;
                _resp_statu = 414;  //URI TOO LONG
                return false;
            }
            if (line == "\n" || line == "\r\n") //说明读到了空行
                break;
            bool ret = ParseHttpHead(line);
            if (ret == false)
                return false;
        }
        //头部处理完毕，进入正文获取阶段
        _recv_statu = RECV_HTTP_BODY;
        return true;
    }
    //接收请求正文
    bool RecvHttpBody(Buffer* buf)
    {
        if (_recv_statu != RECV_HTTP_BODY)
            return false;
        //1。获取正文长度
        size_t content_length = _request.ContentLength();
        if (content_length == 0)
        {
            //没有正文，则请求接收解析完毕
            _recv_statu = RECV_HTTP_OVER;
            return true;
        }
        //2。当前已经接收了多少正文，其实就是往_request._body中放了多少数据
        size_t real_len = content_length - _request._body.size();   //实际还要接收的正文长度
        //3。接收正文放到body中，但是也要考虑当前缓冲区中的数据，是否是全部的正文
        //3.1 缓冲区中数据，包含了当前请求的所有正文，则取出所需数据。缓冲区中也有可能包含下一个协议的内容
        if (buf->ReadAbleSize() >= real_len)
        {
            _request._body.append(buf->ReadPosition(), real_len);
            buf->MoveReadOffset(real_len);
            _recv_statu = RECV_HTTP_OVER;
            return true;
        }
        //3.2 缓冲区中数据，无法满足当前正文的需要，数据不足，取出数据，然后等待新数据到来
        _request._body.append(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        return true;
    }
public:
    HttpContext()
    :_resp_statu(200), _recv_statu(RECV_HTTP_LINE)
    {}
    void ReSet()
    {
        _resp_statu = 200;
        _recv_statu = RECV_HTTP_LINE;
        _request.ReSet();
    }
    //获取相应状态码
    int RespStatu()
    {
        return _resp_statu;
    }
    //获取接收状态
    HttpRecvStatu RecvStatu()
    {
        return _recv_statu;
    }
    //获取已经得到的请求信息
    HttpRequest& Request()
    {
        return _request;
    }
    //接受并解析HTTP请求
    void RecvHttpRequest(Buffer* buf)
    {
        //不同的状态做不同的事情，但是这里不要break，因为处理完请求后，应该立即处理头部，而不是退出等新数据
        switch(_recv_statu)
        {
        case RECV_HTTP_LINE:
            RecvHttpLine(buf);
        case RECV_HTTP_HEAD:
            RecvHttpHead(buf);
        case RECV_HTTP_BODY:
            RecvHttpBody(buf);
        }
    }
};


#define DEFAULT_TIMEOUT 10

class HttpServer
{
private:
    using Handler = std::function<void(const HttpRequest&, HttpResponse*)>;
    using Handlers = std::vector<std::pair<std::regex, Handler>>;
    //有四个功能性请求的路由表
    Handlers _get_route;
    Handlers _post_route;
    Handlers _put_route;
    Handlers _delete_route;
    std::string _basedir;   //静态资源根目录
    TcpServer _server;
private:
    void ErrorHandler(const HttpRequest& req, HttpResponse* rsp)
    {
        //1。组织一个错误展示页面
        std::string body;
        body += "<html>";
        body += "<head>";
        body += "<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>";
        body += "</head>";
        body += "<body>";
        body += "<h1>";
        body += std::to_string(rsp->_statu);
        body += " ";
        body += Util::StatuDesc(rsp->_statu);
        body += "</h1>";
        body += "</body>";
        body += "</html>";
        //2.将页面数据，当做响应正文，放入rsp中
        rsp->SetContent(body, "text/html");
    }
    //将HttpResponse中的要素按照http协议格式进行组织，发送
    void WriteResponse(const PtrConnection& conn, const HttpRequest& req, HttpResponse& rsp)
    {
        //1.先完善头部字段 -- 这几个是几乎必要的头部字段
        if(req.Close() == true)
            rsp.SetHeader("Connection", "close");
        else 
            rsp.SetHeader("Connection", "keep-alive");
        if (rsp._body.empty() == false && rsp.HasHeader("Content-Length") == false)//如果没有设置长度，则要设置长度
            rsp.SetHeader("Content-Length", std::to_string(rsp._body.size()));
        if (rsp._body.empty() == false && rsp.HasHeader("Content-Type") == false)//如果没有设置Content-Type，则要设置
            rsp.SetHeader("Content-Type", "application/octet-stream");
        if (rsp._redirect_flag == true)
            rsp.SetHeader("Location", rsp._redirect_url);
        //2。将rsp中的要素，按照http协议格式进行组织
        std::stringstream rsp_str;  //技巧：快速的拼接字符串
        rsp_str << req._version + " " << std::to_string(rsp._statu) << " " << Util::StatuDesc(rsp._statu) << "\r\n";
        for (auto& head : rsp._headers)
        {
            rsp_str << head.first << ":" << head.second << "\r\n";
        }
        rsp_str << "\r\n";
        rsp_str << rsp._body;
        //3。发送数据
        conn->Send(rsp_str.str().c_str(), rsp_str.str().size());
    }
    //是否是一个静态资源请求
    bool IsFileHandler(const HttpRequest& req)
    {
        //1.必须设置了静态资源根目录
        if (_basedir.empty())
            return false;
        //2.请求方法必须是GET/HEAD方法 -- 只有这两个才是获取实体资源的请求 POST/PUT/DELETE通常更多是功能性的请求
        if (req._method != "GET" && req._method != "HEAD")
            return false;
        //3.请求的资源路径必须是一个合法的路径
        if (Util::ValidPath(req._path) == false)
            return false;
        //4.请求的资源必须存在，且是一个普通文件
        std::string req_path = _basedir + req._path;
        if (req._path.back() == '/')
            req_path += "index.html";
        if (Util::IsRegular(req_path) == false)
            return false;
        return true;
    }
    //静态资源的请求处理
    void FileHandler(const HttpRequest& req, HttpResponse* rsp)
    {
        std::string req_path = _basedir + req._path;
        if (req._path.back() == '/')
        {
            req_path += "index.html";
        }
        bool ret = Util::ReadFile(req_path, &(rsp->_body));
        if (ret == false)
            return;
        std::string mime = Util::ExMime(req_path);  //通过扩展名来获取它的mime
        rsp->SetHeader("Content-Type", mime);
        return;
    }
    //功能性请求的分类处理
    void Dispatcher(HttpRequest& req, HttpResponse* rsp, Handlers& handlers)
    {
        //在对应请求方法的路由表中，查找对应是否含有对应资源请求的处理函数，有则调用，没有则返回404
        //思想：路由表存储的是键值对<正则表达式, 处理函数>
        //使用正则表达式，对请求的资源路径进行正则匹配，匹配成功就使用对应函数进行处理
        //为什么用正则表达式，而不用字符串？因为查询路径，可能有很多种情况，我们不能全写进路由表
        for (auto& handler : handlers)//将每一个正则表达式拿出来进行与查询路径进行匹配
        {
            const std::regex& re = handler.first;
            const Handler& functor = handler.second;
            bool ret = std::regex_match(req._path, req._matches, re);
            if (ret == false)
                continue;
            return functor(req, rsp);   //传入请求信息和空的rsp，执行处理函数
        }
        rsp->_statu = 404;
    }
    void Route(HttpRequest& req, HttpResponse* rsp)
    {
        //对请求进行分析，是一个静态资源请求还是一个功能性请求
        //  静态资源请求，则进行静态资源的处理
        //  功能性请求，则需要通过几个请求路由表来确定是否有处理函数
        //  既不是静态资源请求，也没有设置对应的功能请求处理函数，就返回405
        if (IsFileHandler(req) == true)
        {
            //是一个静态资源请求，则进行静态资源请求的处理
            return FileHandler(req, rsp);
        }
        //走到这里，则说明是功能性请求
        if (req._method == "GET" || req._method == "HEAD")
            return Dispatcher(req, rsp, _get_route);
        else if (req._method == "POST")
            return Dispatcher(req, rsp, _post_route);
        else if (req._method == "PUT")
            return Dispatcher(req, rsp, _put_route);
        else if (req._method == "DELETE")
            return Dispatcher(req, rsp, _delete_route);
        rsp->_statu = 405;  //Method Not Allowed
    }
    //设置上下文
    void OnConnected(const PtrConnection& conn)
    {
        conn->SetContext(HttpContext());
        DBG_LOG("NEW CONNECTION %p", conn.get());
    }
    //缓冲区数据解析 + 处理
    void OnMessage(const PtrConnection& conn, Buffer* buffer)
    {
        while (buffer->ReadAbleSize() > 0)
        {
            //1。获取上下文
            HttpContext* context = conn->GetContext()->get<HttpContext>();
            //2。通过上下文对缓冲区数据进行解析，得到HttpRequest对象
            //a。如果缓冲区的数据解析出错，就直接回复出错响应
            //b。如果解析正常，且请求已经获取完毕，才开始进行处理
            context->RecvHttpRequest(buffer);
            HttpRequest& req = context->Request();
            HttpResponse rsp(context->RespStatu());
            if (context->RespStatu() >= 400)    //代表数据解析出错
            {
                //进行错误响应，关闭连接
                ErrorHandler(req, &rsp);        //填充一个错误显示页面数据到rsp中
                WriteResponse(conn, req, rsp);   //组织响应发送给客户端
                context->ReSet();
                buffer->MoveReadOffset(buffer->ReadAbleSize()); //出错了就把缓冲区数据清空
                conn->Shutdown();   //关闭连接
                return;
            }
            if (context->_recv_statu != RECV_HTTP_OVER)//代表这不是一个完整的请求
                return;
            //3。路由请求 + 业务处理
            Route(req, &rsp);
            //4。对HttpResponse进行组织发送
            WriteResponse(conn, req, rsp);
            //5。重置上下文
            context->ReSet();
            //6。根据长短连接判断是否关闭连接或者继续处理 -- 短连接：接收处理一次服务器就把连接关闭     长连接：一直与你通信
            if (rsp.Close() == true)
                conn->Shutdown();
        }
    }
public:
    HttpServer(int port, int timeout = DEFAULT_TIMEOUT)
    :_server(port)
    {
        _server.EnableInactiveRelease(timeout);
        _server.SetConnectedCallback(std::bind(&HttpServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&HttpServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void SetBaseDir(const std::string& path)
    {
        assert(Util::IsDirectory(path) == true);
        _basedir = path;
    }
    //设置/添加，请求与处理函数的映射关系
    void Get(const std::string& pattern, const Handler& handler)
    {
        _get_route.push_back(std::make_pair(std::regex(pattern), handler));
    }
    void Post(const std::string& pattern, const Handler& handler)
    {
        _post_route.push_back(std::make_pair(std::regex(pattern), handler));
    }
    void Put(const std::string& pattern, const Handler& handler)
    {
        _put_route.push_back(std::make_pair(std::regex(pattern), handler));
    }
    void Delete(const std::string& pattern, const Handler& handler)
    {
        _delete_route.push_back(std::make_pair(std::regex(pattern), handler));
    }
    void SetThreadCount(int count)
    {
        _server.SetThreadCount(count);
    }
    void Listen()
    {
        _server.Start();
    }
};